<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status2']=array(
	array('id'=>1,'title'=>'待发货'),
	array('id'=>2,'title'=>'待收货'),
	array('id'=>3,'title'=>'已完成'),
	array('id'=>4,'title'=>'已关闭'),
	array('id'=>5,'title'=>'申请维权'),
	array('id'=>6,'title'=>'已维权'),
);
if($_RQ['op']=='list'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！请选择订单!','error');
	}
	$item=pdo_get('jy_weishop_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','status','is_peisong','dispatchid','dispatchprice','sendtype','storeid','merchid','address','addressid','ordersn','remark','price','isdish'));
	$params=array('orderid'=>$id,'uniacid'=>$_SESSION['uniacid']);
	$list=pdo_getall('jy_weishop_order_express',$params);
	foreach($list as &$gv){
		$gv['ordersn']=$item['ordersn'];
		$gv['goodsinfo']=json_decode($gv['goodsinfo'],true);
		$gv['goodsinfo']=array_values($gv['goodsinfo']);
		$gv['title']=$gv['goodsinfo'][0]['title']?:'无';
		$gv['selespename']=$gv['goodsinfo'][0]['selespename']?:'无';
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			//array('op'=>'list','query'=>'&id='.$item['id'],'title'=>'发货记录'),
			array('action'=>'/order.list/fa/id='.$item['id'],'title'=>'前往发货>>'),
		),
		'list'=>array('data'=>'list','edit'=>true,'del_one'=>false,'delall'=>false,'list'=>array(
			array('type'=>'radio','title'=>'物流选择','name'=>'fatype','options'=>'fatype'),
			array('type'=>'select','title'=>'快递公司','name'=>'express_id','options'=>'express','option_title'=>'name'),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn'),
			array('type'=>'string','title'=>'发货内容','name'=>'content'),
			array('type'=>'string','title'=>'商品名称','name'=>'title'),
			array('type'=>'string','title'=>'规格','name'=>'selespename'),
			//array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),		
			array('name'=>'createtime','title'=>'时间','type'=>'time'),	
		),'action'=>array(
			array('type'=>'modal','op'=>'look','title'=>'发货详情'),
			array('type'=>'href','action'=>'/order.express//isexpress=1&nomodal=1','title'=>'查看物流_Express'),		
		)),
	);
	$return['fatype']=array('快递配送','其他物流','无需物流');
	$return['express']=$this->model('model')->express_types();
	$return['htmls']=$htmls;
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='edit' || $_RQ['op']=='look'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['sendtype']==1){
			if($post['is_peisong']){
				if($post['need_setpeisong'] && !$post['peisong_id']){
					exi('请选择配送员！','error');
				}
			}else{
				if(!$post['express_id'] || !$post['expresssn']){
					exi('请填写快递信息','error');
				}
			}
			$data=array(
				'fatype'=>$post['fatype'],
				'content'=>$post['content'],
				'need_setpeisong'=>$post['need_setpeisong'],
				'peisong_id'=>$post['peisong_id'],
				'verifycode'=>$post['verifycode'],
				'express_id'=>$post['express_id'],
				'expresssn'=>$post['expresssn']
			);
			pdo_update('jy_weishop_order_express',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));		
		}
		exi('修改物流信息成功！！','','/order.fachange//id='.$post['orderid']);
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order_express',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	$order=pdo_get('jy_weishop_order',array('id'=>$item['orderid'],'uniacid'=>$_SESSION['uniacid']),array('id','status','is_peisong','dispatchid','dispatchprice','sendtype','storeid','merchid','address','addressid','ordersn','remark','price','isdish'));
	$item['sendtype']=$item['sendtype']?:$order['sendtype'];
	if(!$item['address']){
		$item['address']=$order['address'];
		$item['address']=json_decode($item['address'],true);
		if($item['address'] && ($item['address']['address'] || $item['address']['mobile'])){
			$address=$item['address'];
			$addressid=$item['address'];
		}else{
			$address=pdo_get('core_address',array('id'=>$item['addressid']));
			$addressid=$item['addressid'];
		}
		$item['address']=array(
			'address'=>model('core/member')->get_address_str($addressid),
			'mobile'=>$address['mobile'],
			'name'=>$address['name'],
		);
	}else{
		$item['address']=json_decode($item['address'],true);
	}
	if($item['goodsinfo']){
		$item['goodsinfo']=json_decode($item['goodsinfo'],true);
	}else{
		$params=array('orderid'=>$item['orderid'],'uniacid'=>$_SESSION['uniacid']);
		if($item['ordergoodsid']){
			$ordergoodsid=str_array($item['ordergoodsid']);
			$params['goodsid']=$ordergoodsid;
		}
		$item['goodsinfo']=pdo_getall('jy_weishop_order_goods',$params,array('status','optionid','total','goodsid','istaocan','api_pluginname','api_id','id'));
		foreach($item['goodsinfo'] as &$gv){
			$gv['title']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$gv['goodsid']),'title');
			//商品规格
			if($gv['optionid']){
				$options=pdo_get('jy_weishop_goods_option',array('id'=>$gv['optionid']),array('title','specs'));
				if($gv['istaocan']){
					$gv['selespename']=$options['title'];
				}else{
					$specs=$options['specs'];
					$specs=explode("_",$specs);
					foreach($specs as &$spec){
						$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
					}
					$gv['selespename']=implode("，",$specs);
				}
			}else{
				$gv['selespename']='无';
			}
		}
	}
	
	$htmls=array(
		'type'=>$_RQ['op']=='look'?'look':'edit',
		'tabs'=>array(
			array('action'=>'/order.list/fa/id='.$order['id'],'title'=>'订单发货'),
			array('action'=>'/order.fachange//id='.$order['id'],'title'=>'发货记录'),
		),
		'desc'=>'修改发货',
	);
	$edit=array(
			array('type'=>'copy','title'=>'运费','name'=>'sendtitle','show'=>array('sendtype'=>1)),
			array('type'=>'copy','title'=>'联系人','name'=>'address.name','show'=>array('sendtype'=>1)),
			array('type'=>'copy','title'=>'联系电话','name'=>'address.mobile','show'=>array('sendtype'=>1)),
			array('type'=>'copy','title'=>'收货地址','name'=>'address.address','show'=>array('sendtype'=>1)),
			array('type'=>'array','name'=>'goodsinfo','title'=>'商品详情','noadd'=>1,'nodel'=>1,'nosplice'=>1,'noedit'=>1,
                'list'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'string','title'=>'数量','name'=>'total','read'=>true),
				),
				'details'=>array(
					array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
					array('type'=>'string','title'=>'规格','name'=>'selespename','read'=>true),
					array('type'=>'string','title'=>'数量','name'=>'total','read'=>true),
				),
            ),
			array('type'=>'radio','title'=>'物流选择','name'=>'fatype','options'=>'fatype','show'=>array('sendtype'=>1)),
			array('type'=>'select','title'=>'快递公司','name'=>'express_id','options'=>'express','option_title'=>'name','show'=>array('sendtype'=>1,'fatype'=>0)),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('sendtype'=>1,'fatype'=>0)),
			array('type'=>'string','title'=>'物流内容','name'=>'content','show'=>array('sendtype'=>1,'fatype'=>array(1,2))),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定配送员？','show'=>array('sendtype'=>1,'is_peisong'=>1),'help'=>'不指定，则由配送员自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定配送员','name'=>'peisong_id','options'=>'peisongs','option_value'=>'mid','show'=>array('need_setpeisong'=>1),'help'=>'只能指定该门店下面的配送员，或本城市的平台配送员！'),
			array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2)),
			array('type'=>'string','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'=>3),'help'=>'虚拟物品发货，填写发货内容！'),
	);
	if($_RQ['op']=='look'){
		$htmls['look']=$edit;
		unset($htmls['tabs']);
	}else{
		$htmls['edit']=$edit;
	}
	$return['fatype']=array('快递配送','其他物流','无需物流');
	$return['express']=$this->model('model')->express_types();
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
?>